
((_global_)=>{
	if(!Reflect.has(_global_, 'mtec')){
		Reflect.set(_global_, 'mtec', {});
		Reflect.set(mtec, '__mtec__', 'Many technologies');
	}

	if(!mtec.log){
		Reflect.set(mtec, 'log', {});
	}
	Reflect.set(mtec.log, '__space_description__', 'This is a mtec subnamespace for log');
})(window ?? globalThis);

/** debug模式 */
var DEBUG_MODE = true;

mtec.log.enable_debug_mode = function(enable){
	DEBUG_MODE = enable;
}

mtec.log.tag = function(tag, ...args){
	//return void 0;
	let str = '', colors = [];

	if(typeof(tag)=='string' && tag.includes(':')){
		let joint_call: (item: string[], index: number)=>void;

		if(DEBUG_MODE){
			joint_call = function(item, index){
				str += '%c' + item[0];
				let [h, s, l] = mtec.color.toHSLColor(item[1]);
				let clr = [
					['background-color', mtec.color.normalizeHexColor(item[1])],
					['color', mtec.color.HSL2Hex(h, s>0 ? 100 : 0, l>50 ? 20 : 80)]
				].reduce((t, el)=>{
					t.push(el.join(':'));
					return t;
				}, [
					'padding:0 0.5em',
					'font-weight: bolder',
				]).join('; ') + '; ';
				colors.push(clr);

				h = s = l = clr = undefined;
			}
		}else{
			joint_call = function(item, index){
				str += (index==0 ? '' : ' ') + item[0];
			}
		}

		tag.split(';').map(s=>{
			s = s.trim();
			if(s.length==0) return [];
			else return s.split(':').map(kv=>kv.trim());
		}).filter(item=>item.length==2 && item.every(kv=>kv.length>0))
		.forEach((el, idx)=>joint_call(el, idx));

		joint_call = undefined;
	}

	if(DEBUG_MODE){
		colors[0] += 'border-top-left-radius: 0.5em 0.5em; border-bottom-left-radius: 0.5em 0.5em; ';
		colors[colors.length-1] += 'border-top-right-radius: 0.5em 0.5em; border-bottom-right-radius: 0.5em 0.5em; ';

		console.groupCollapsed(str + '%c', ...colors, '', ...args);
		console.trace();
		console.groupEnd();
	}else{
		if(args.length>0){
			args.forEach(param=>{
				str += ' ';
				switch(typeof param){
					case 'object': str += JSON.stringify(param); break;
					case 'function': str += param.toString(); break;
					default: str += param; break;
				}
			});
		}

		console.log('[LOG@TAG]:', str);
	}
}

mtec.log.warn = function(...args){
	mtec.log.tag("WARN:orange;>>:tomato", ...args);
}